package com.platform.api.modular.amqp.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.platform.api.modular.demo.param.DemoParam;
import com.platform.core.annotion.BusinessLog;
import com.platform.core.annotion.Permission;
import com.platform.core.enums.LogAnnotionOpTypeEnum;
import com.platform.core.pojo.response.ResponseData;
import com.platform.core.pojo.response.SuccessResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "消息队列测试模块")
@RestController
public class AmqpDemoController {

    @Autowired
    private RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法


    /**
     * 数据访问
     * @return
     */
    @ApiOperation(value = "消息发送")
    @GetMapping("/amqp/send")
    public ResponseData send() {

        // 创建一个Snowflake对象，参数为工作机器ID，可以通过配置或某种方式获取，这里示例使用0和0
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        for (int i = 0; i < 1000; i++) {
            // 生成一个ID
            String id = snowflake.nextIdStr();
            Map<String,Object> map = new HashMap<>();
            map.put("id",id);
            map.put("name","豫东一棵草");
            map.put("phone","123123123");
            map.put("num",i);
            String str = JSONObject.toJSONString(map);
            Message msg = MessageBuilder.withBody(str.getBytes()).setMessageId(id).build();
            CorrelationData correlationData = new CorrelationData(id);
            rabbitTemplate.convertAndSend("", "goods2CQueueTest", msg, correlationData);
        }
        return new SuccessResponseData();
    }


}
